package com.et114.modules.myet.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.et114.components.acegi.Constants;
import com.et114.core.action.BaseDispatchAction;
import com.et114.modules.cust.services.IMng_CustService;
import com.et114.modules.cust.services.Mng_CustServiceImp;
import com.et114.modules.cust.vo.CustBaseVo;
import com.et114.modules.myet.form.MyOrderFrom;
import com.et114.modules.myet.service.IMyEt_UCenterService;
import com.et114.modules.myet.service.MyEt_UCenterServiceImp;
import com.et114.modules.myet.vo.MyEtOrder_VO;
import com.et114.modules.order.service.IMng_OrderService;
import com.et114.modules.order.service.Mng_OrderServiceImp;
import com.et114.modules.order.vo.OrderTrack;
import com.et114.modules.products.commons.Const;
import com.et114.modules.products.commons.SessionUser;

/**
 * 我的订单
 * 
 * @author zhiweip
 * @version 1.0
 * @created 22-十二月-2010 13:33:56
 */
public class MyEt_orderAction extends MyEtAction {
	
	private IMyEt_UCenterService	m_IMyEt_UCenterService	= ( MyEt_UCenterServiceImp ) getBean ( "m_IMyEt_UCenterService" );
	private IMng_OrderService		mng_OrderService		= ( Mng_OrderServiceImp ) getBean ( "mng_OrderService" );
	
	public MyEt_orderAction ( ) {
		
	}
	
	public void finalize ( ) throws Throwable {
		
	}
	/**
	 * 订单查询
	 */
	public ActionForward unspecified ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception {
		if ( isNeedLogin ( request ) ) {  response.sendRedirect ( "login.html" );return mapping.findForward ( "" );}
		MyOrderFrom mForm = ( MyOrderFrom ) form;
		MyEtOrder_VO vo = new MyEtOrder_VO ( );
		vo.setPageSize ( mForm.getPageSize ( ) );
		vo.setPageNum ( mForm.getPageNum ( ) );
		vo.setCustid ( getCustId ( request ).toString ( ) );
		String lang=getSessionLanguage ( request );
		vo.setLanguage ( lang );
		request.setAttribute ( "DataList" , m_IMyEt_UCenterService.myOrder ( vo ) );

		return mapping.findForward ( "orderquery" );
	}
	
	/**
	 * 已成交订单查询
	 */
	public ActionForward orderQuery ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception {
		return mapping.findForward ( "orderquery" );
	}
	
	/**
	 * 订单明细
	 */
	public ActionForward orderView ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception {
		if ( isNeedLogin ( request ) ) {  response.sendRedirect ( "login.html" );return mapping.findForward ( "" );}
		
		IMng_CustService mng_CustService = ( Mng_CustServiceImp ) getBean ( "mng_CustService" );
		
		
		OrderTrack order = new OrderTrack ( );	
		String lang=getSessionLanguage ( request );
		order.setLanguage ( lang );
		
		String orderid = request.getParameter ( "orderid" );		
		if ( orderid != null ){
			order.setOrderid ( new Integer ( orderid ) );
			order.setOperType ( "cust" );
			OrderTrack o = (OrderTrack)mng_OrderService.viewOrder ( order );
			if(o.getCustid ( ).compareTo (getCustId ( request ))!=0){
				throw new Exception(  Constants.AUTH_ACCESSDENIED );
			}
			CustBaseVo cust = new CustBaseVo ( );
			cust.setCustid ( o.getCustid ( ) );
			cust = mng_CustService.loadCust ( cust );
			if(cust == null){ 
				request.setAttribute ( "msg" , "Customer Lost!" );
				request.setAttribute ( "result" , "error" );
				return mapping.findForward ("et_info");
			}
			
			OrderTrack info =(OrderTrack) mng_OrderService.loadOrderInfo ( order );
			logger.debug ("###########"+info.getBillcode ( )+"orderid="+orderid);
			List < OrderTrack > dList = mng_OrderService.loadOrderDetail ( order );
			
			request.setAttribute ( "ORDERDETAIL" , dList );
			request.setAttribute ( "OrderBase" , o );
			request.setAttribute ( "OrderInfo" , info );			
			request.setAttribute ( "BASEINFO" , cust );
		}
		return mapping.findForward ( "orderview" );
	}
	
	/**
	 * 订单确认,准备生成订单
	 */
	public ActionForward confirmOrder ( ActionMapping mapping ,
			ActionForm form , HttpServletRequest request ,
			HttpServletResponse response ) throws Exception {
		return mapping.findForward ( "queryDataList_read" );
	}
	
	/**
	 * 生成订单
	 */
	public ActionForward newOrder ( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response )
			throws Exception {
		return mapping.findForward ( "queryDataList_read" );
	}
	
}